package com.viewpagertext.fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.StarredSongDao;
import com.viewpagertext.DBHelper.Dao.UserDao;
import com.viewpagertext.Model.User;
import com.viewpagertext.R;
import com.viewpagertext.activitys.MainActivity;



public class RegisterFragment extends Fragment {

    private TextView userLoginBtn;
    private TextView registerBtn;
    private EditText passwordEdit;
    private EditText usernameEdit;
    private EditText passwordRepeatEdit;
    UserDao helper = AppDatabase.getInstance().UserDao();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_register, null);
        userLoginBtn = v.findViewById(R.id.userLoginBtn);
        registerBtn = v.findViewById(R.id.registerBtn);
        usernameEdit = v.findViewById(R.id.usernameEdit);
        passwordEdit = v.findViewById(R.id.passwordEdit);
        passwordRepeatEdit = v.findViewById(R.id.passwordRepeatEdit);
        userLoginBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                fm.beginTransaction()
                        //替换为LoginFragment
                        .replace(R.id.frameContain, new LoginFragment())
                        .commit();
            }
        });
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = String.valueOf(passwordEdit.getText());
                String passwordRepeat = String.valueOf(passwordRepeatEdit.getText());
                String username = String.valueOf(usernameEdit.getText());
                System.out.println("密码" + passwordRepeat + "  " + password);
                if (!password.equals(passwordRepeat)) {
                    Toast.makeText(getActivity(), "两次密码需要一致", Toast.LENGTH_SHORT).show();
                } else {
                    if (helper.getUserByusername(username) != null) {
                        Toast.makeText(getActivity(), "该用户名已存在", Toast.LENGTH_SHORT).show();
                    } else {
                        helper.insertUser(username, password);
                        Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();
                        FragmentManager fm = getActivity().getSupportFragmentManager();
                        fm.beginTransaction()
                                //替换为LoginFragment
                                .replace(R.id.frameContain, new LoginFragment())
                                .commit();
                    }

                }
            }

            //进行注册功能

        });

        return v;
    }

}
